﻿using System;
using System.Collections.Generic;
using System.Web;
using Hxj.Common;
using Hxj.Web;
using System.IO;
using System.Text;
using System.Data;
using RicciWebsite.Model;


namespace RicciWebsite
{
    public class Common
    {

        #region Cache

        /// <summary>
        /// 默认缓存12个小时
        /// </summary>
        public static Hxj.Data.Cache DefaultCache = new Hxj.Data.Cache(43200);

        #endregion


        #region 验证码

        /// <summary>
        /// 验证码COOKIENAME
        /// </summary>
        public const string VerifycodeCookieName = "verifycode";


        /// <summary>
        /// 保存验证码cookie
        /// </summary>
        /// <param name="value"></param>
        public static void SetVerifycodeCookie(string value)
        {
            Cookie.SetDefaultCookie(VerifycodeCookieName, Encrypt.Encode(value, varifycodedecryptkey));
        }

        /// <summary>
        /// 验证码加密Key
        /// </summary>
        private const string varifycodedecryptkey = "k&^wsl[.~#!";

        /// <summary>
        /// 读取验证码
        /// </summary>
        /// <returns></returns>
        public static string GetVerifycodeCookie()
        {
            return Encrypt.Decode(Cookie.GetDefaultCookieValue(VerifycodeCookieName), varifycodedecryptkey);
        }

        #endregion


        #region 用户信息

        public const string UserIDCookieName = "userid";

        /// <summary>
        /// 设置用户ID Cookie
        /// </summary>
        /// <param name="userid"></param>
        public static void SetUserIDCookie(string userid)
        {
            Cookie.SetDefaultCookie(UserIDCookieName, Encrypt.Encode(userid, useriddecryptkey));
        }
        /// <summary>
        /// 用户ID加密key
        /// </summary>
        private const string useriddecryptkey = "?<>0(*&%#@~hjew";

        /// <summary>
        /// 读取用户ID
        /// </summary>
        /// <returns></returns>
        public static string GetUserIDCookie()
        {
            return Encrypt.Decode(Cookie.GetDefaultCookieValue(UserIDCookieName), useriddecryptkey);
        }

        #endregion


        #region 语言

        /// <summary>
        /// 语言
        /// </summary>
        public enum Language
        {
            SampleChinese, TraditionalChinese, English
        }

        /// <summary>
        /// 设置语言
        /// </summary>
        /// <param name="lan"></param>
        public static void SetLanguage(Language lan)
        {
            Cookie.SetCookie("language", lan.ToString());
        }

        /// <summary>
        /// 读取语言
        /// </summary>
        /// <param name="lan"></param>
        public static Language GetLanguage()
        {
            try
            {
                return Hxj.Common.EnumHelper.GetInstance<Language>(Cookie.GetCookieValue("language"));
            }
            catch
            {
                string defaultlanguage = HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
                //zh-cn,zh-tw, en

                switch (defaultlanguage)
                {
                    case "zh-cn":
                        return Language.SampleChinese;
                    case "zh-tw":
                        return Language.TraditionalChinese;
                    default:
                        return Language.English;
                }


            }
        }

        /// <summary>
        /// 获取语言
        /// </summary>
        /// <returns></returns>
        public static string GetLanguageWords()
        {
            RicciWebsite.Common.Language lan = RicciWebsite.Common.GetLanguage();
            switch (lan)
            {
                case Common.Language.SampleChinese:
                    return "zh-cn";
                case Common.Language.TraditionalChinese:
                    return "zh-tw";
                case Common.Language.English:
                    return "en";
                default:
                    break;
            }

            return "en";
        }

        #endregion


        /// <summary>
        /// 产品状态
        /// </summary>
        public enum ProductsStatus
        {
            未发布 = 0, 已发布 = 1, 推荐 = 2
        }

        /// <summary>
        /// 新闻状态
        /// </summary>
        public enum NewsStatus
        {
            未发布 = 0, 已发布 = 1, 置顶 = 2
        }

        /// <summary>
        /// 获取MSN和URL
        /// </summary>
        /// <returns></returns>
        public static string GetMsnAndUrl()
        {
            config configInfo = configHelper.Get();
            if (string.IsNullOrEmpty(configInfo.Msn))
                return string.Empty;

            System.Text.StringBuilder msnurl = new System.Text.StringBuilder();
            msnurl.Append("MSN：");
            if (!string.IsNullOrEmpty(configInfo.Msnurl))
            {
                msnurl.Append("<a target='_blank' href='");
                msnurl.Append(configInfo.Msnurl.Replace("&mkt=", "&"));
                msnurl.Append("&mkt=");
                msnurl.Append(GetLanguageWords());
                msnurl.Append("'>");
            }
            msnurl.Append(configInfo.Msn);
            if (!string.IsNullOrEmpty(configInfo.Msnurl))
            {
                msnurl.Append("</a>");
            }

            return msnurl.ToString();
        }


        #region 公司简介

        /// <summary>
        /// 公司简介路径
        /// </summary>
        private static string introductionPath = HttpContext.Current.Server.MapPath("~/config/introduction.txt");



        private static object lockObj = new object();

        /// <summary>
        /// 返回公司简介
        /// </summary>
        /// <returns></returns>
        public static string GetIntroduction()
        {
            string introduction = DefaultCache.GetCache("introduction_config") as string;

            if (null == introduction)
            {
                lock (lockObj)
                {
                    if (null == introduction)
                    {
                        introduction = File.ReadAllText(introductionPath, Encoding.UTF8);
                        DefaultCache.AddCacheFilesDependency("introduction_config", introduction, introductionPath);
                    }
                }
            }

            return introduction;
        }


        /// <summary>
        /// 设置公司简介
        /// </summary>
        /// <returns></returns>
        public static void SetIntroduction(string content)
        {
            File.WriteAllText(introductionPath, content, Encoding.UTF8);
        }


        #endregion


        /// <summary>
        /// 绑定产品分类
        /// </summary>
        /// <param name="ddl"></param>
        public static void BindCategory(System.Web.UI.WebControls.DropDownList ddl)
        {
            DataTable catedt = RicciDatabase.Default.From<data_products_categories>().Where(data_products_categories._.catetype == 1).ToDataTable();

            Hxj.Web.WebUtils.BuildDropDownList(ddl, catedt, data_products_categories._.pcname.PropertyName, data_products_categories._.pcid.PropertyName,
                data_products_categories._.ppcid.PropertyName, data_products_categories._.pcid.PropertyName, new Guid().ToString(), data_products_categories._.psort.PropertyName);
        }

    }
}
